package kr.co.bettersoft.sendme.provider;

import java.util.ArrayList;

import kr.co.bettersoft.sendme.activity.SendMeAudioPlayForReceiveMessageActivity;
import kr.co.bettersoft.sendme.activity.SendMeReceiveVoiceListActivity;
import kr.co.bettersoft.sendme.activity.SendMeTempMessageListActivity;
import kr.co.bettersoft.sendme.util.SendMeCommon;
import kr.co.bettersoft.sendme.activity.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SendMeReceiveMessageForPlayerSlideMenuAdapter extends BaseAdapter {
	
	private Context context;
	
	private ArrayList<SendMeVoice> receiveMessageList;
	
	private SendMeVoice receiveMessage;
	LayoutInflater layoutInflater;
	
	private SendMeAudioPlayForReceiveMessageActivity receiveSendMeAudioPlayForReceiveMessageActivity;
	
	SendMeCommon sendMeCommon;
	
	String TAG = "SendMeReceiveMessageAdapter";
	
	View view;
	
	ImageButton receiveMessagePlayBtn;		// receiveMessagePlayBtn	receiveMessageReplyBtn	receiveMessageBlockBtn
//	ImageButton receiveMessageReplyBtn;
//	ImageButton receiveMessageBlockBtn;
//	ImageButton menuDeleteBtn;
	
	public SendMeReceiveMessageForPlayerSlideMenuAdapter(Context context, SendMeAudioPlayForReceiveMessageActivity sendMeAudioPlayForReceiveMessageActivity, ArrayList<SendMeVoice> receiveMessageList) {
		this.context = context;
		this.receiveMessageList = receiveMessageList;
		this.receiveSendMeAudioPlayForReceiveMessageActivity = sendMeAudioPlayForReceiveMessageActivity;
		
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		sendMeCommon = new SendMeCommon();
	}

	


		
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if(receiveMessageList!=null){
			return receiveMessageList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return receiveMessageList.get(position);
	}


	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.activity_send_me_play_for_receive_message_left_slide_menu_adapter, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		receiveMessage = receiveMessageList.get(position);		
		
		receiveMessagePlayBtn = (ImageButton) view.findViewById(R.id.receiveMessagePlayBtn);
//		receiveMessageReplyBtn = (ImageButton) view.findViewById(R.id.receiveMessageReplyBtn);
//		receiveMessageBlockBtn = (ImageButton) view.findViewById(R.id.receiveMessageBlockBtn);
//		menuDeleteBtn = (ImageButton) view.findViewById(R.id.menuDeleteBtn);					// 삭제 버튼	// 밀어서 지우기 용도
		receiveMessagePlayBtn.setTag(position);
//		receiveMessageReplyBtn.setTag(position);
//		receiveMessageBlockBtn.setTag(position);
//		menuDeleteBtn.setTag(position);
		
		receiveMessagePlayBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				if(receiveMessageList.get(selectedPosition).getExpiredCounter().equals("기간초과")){
					receiveSendMeAudioPlayForReceiveMessageActivity.showAlertMessage("기간이 초과되어 들을수 없습니다");
				}else{
					receiveSendMeAudioPlayForReceiveMessageActivity.playAudioMessage(receiveMessageList.get(selectedPosition));
				}
			}
		});
//		receiveMessageReplyBtn.setOnClickListener(new OnClickListener() {
//		@Override
//		public void onClick(View v) {
//			
//			int selectedPosition = Integer.parseInt(v.getTag()+"");
//			receiveSendMeAudioPlayForReceiveMessageActivity.replyAudioMessage(receiveMessageList.get(selectedPosition));
//
//			}
//		});
//		receiveMessageBlockBtn.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				
//				int selectedPosition = Integer.parseInt(v.getTag()+"");
//				receiveSendMeAudioPlayForReceiveMessageActivity.blockUser(receiveMessageList.get(selectedPosition));
//			}
//		});
//		menuDeleteBtn.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				
//				int selectedPosition = Integer.parseInt(v.getTag()+"");
//				receiveSendMeReceiveVoiceListActivity.confirmDelete(receiveMessageList.get(selectedPosition));		 
//			}
//		});
		
		// 친구 이름을 세팅 한다.
		if(receiveMessage.getSenderName().length()>0){
			wrapper.getTxtViewFriendName().setText(receiveMessage.getSenderName());
		}else{
			wrapper.getTxtViewFriendName().setText(receiveMessage.getSender());
		}
		
		// 친구 전화번호 세팅 한다.	 
		wrapper.getTxtViewFriendPhoneNumber().setText(sendMeCommon.phoneNumberForShowFormat(receiveMessage.getSender()));
		
//		// 삭제 버튼 보일지 여부
//		if(receiveMessage.isShowDeleteBtnYn()){
//			wrapper.getLayoutButtons1().setVisibility(View.GONE);
//			wrapper.getLayoutButtons2().setVisibility(View.VISIBLE);
//		}else{
//			wrapper.getLayoutButtons1().setVisibility(View.VISIBLE);
//			wrapper.getLayoutButtons2().setVisibility(View.GONE);
//		}
//		
		// 읽지 않은 메시지 표시(아이콘)
		if(receiveMessage.getReadYn().equals("N")){
			wrapper.getNewMessageIcon().setVisibility(View.VISIBLE);
		}else{
			wrapper.getNewMessageIcon().setVisibility(View.INVISIBLE);
		}
		
		// 메시지 등록일 표시
		wrapper.getRegisterDate().setText(receiveMessage.getRegisterDate());
//		// 읽은 시각 표시
//		wrapper.getRecognitionModifyDate().setText(receiveMessage.getRecognitionModifyDate());
//		// 유효 기간 표시
//		wrapper.getExpiredCounter().setText(receiveMessage.getExpiredCounter());
		return view;
	}
	
	class ViewWrapper {
		View base;
		
		TextView txtViewFriendName;				// 친구 이름.
		TextView txtViewFriendPhoneNumber;				// 친구 전화번호.
		TextView registerDate;					// 메시지 등록 일
		TextView recognitionModifyDate;					// 음성 메시지 읽은 시각.
		TextView expiredCounter;					// 만료일까지 남은 날짜수
		
		ImageView newMessageIcon;				// 읽지 않은 메시지 아이콘

		View layoutButtons1;			// 기본 버튼 레이아웃
		View layoutButtons2;			// 삭제 버튼 레이아웃
		
		ViewWrapper(View base) {
			this.base = base;
		}
//		
		TextView getTxtViewFriendName() {
			if(txtViewFriendName == null) {
				txtViewFriendName = (TextView)view.findViewById(R.id.txtViewFriendName);
			}
			
			return txtViewFriendName;
		}
		
		TextView getTxtViewFriendPhoneNumber() {
			if(txtViewFriendPhoneNumber == null) {
				txtViewFriendPhoneNumber = (TextView)view.findViewById(R.id.txtViewFriendPhoneNumber);
			}
			
			return txtViewFriendPhoneNumber;
		}
		
		TextView getRegisterDate() {
			if(registerDate == null) {
				registerDate = (TextView)view.findViewById(R.id.registerDate);
			}
			
			return registerDate;
		}
		TextView getRecognitionModifyDate() {		
			if(recognitionModifyDate == null) {
				recognitionModifyDate = (TextView)view.findViewById(R.id.recognitionModifyDate);
			}
			
			return recognitionModifyDate;
		}
		TextView getExpiredCounter() {
			if(expiredCounter == null) {
				expiredCounter = (TextView)view.findViewById(R.id.expiredCounter);
			}
			
			return expiredCounter;
		}
		
		ImageView getNewMessageIcon(){
			if(newMessageIcon == null) {
				newMessageIcon = (ImageView)view.findViewById(R.id.newMessageIcon);
			}
			
			return newMessageIcon;
		}
		
		View getLayoutButtons1() {
			if(layoutButtons1 == null) {
				layoutButtons1 = (View)view.findViewById(R.id.layout_buttons1);
			}
			return layoutButtons1;
		}
		
		View getLayoutButtons2() {
			if(layoutButtons2 == null) {
				layoutButtons2 = (View)view.findViewById(R.id.layout_buttons2);
			}
			return layoutButtons2;
		}
	}

}